<template>
  <el-dialog
    v-model="visible"
    title="添加充值账号"
    width="800px"
    :show-close="false"
  >
    <template #header>
      <div class="dialog-header">
        <span class="dialog-title">添加充值账号</span>
        <div class="dialog-actions">
          <el-button @click="handleCancel"> 取消 </el-button>
          <el-button
            type="primary"
            @click="handleSubmit"
            :loading="loading"
            style="background-color: #039691"
          >
            确定选择
          </el-button>
        </div>
      </div>
    </template>
    <el-form inline>
      <!-- <el-form-item>
        <el-input
          v-model="queryParams.realName"
          placeholder="账户名称"
          clearable
          style="width: 180px"
        />
      </el-form-item> -->
      <el-form-item>
        <el-input
          v-model="queryParams.cid"
          placeholder="账户名称"
          clearable
          style="width: 180px"
        />
      </el-form-item>
      <el-form-item prop="systemType">
        <el-select
          clearable
          v-model="queryParams.systemType"
          placeholder="请选择广告媒体"
          style="width: 200px"
        >
          <el-option
            v-for="item in bus_system_type"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="text" @click="handleSearch"> 查询 </el-button>
        <el-button type="info" link @click="resetSearch"> 重置 </el-button>
      </el-form-item>
    </el-form>
    <el-table
      @selection-change="handleSelectionChange"
      border
      row-key="id"
      :data="list"
      :loading="loading"
    >
      <el-table-column type="selection" reserve-selection />
      <el-table-column type="index" label="序号" width="80" />
      <el-table-column prop="promotionLink" label="推广链接" min-width="120" />
      <el-table-column prop="status" label="状态" width="100">
        <template #default="scope">
          {{ scope.row.status === "1" ? "停用" : "正常" }}</template
        >
      </el-table-column>
      <el-table-column prop="" label="结算币种" width="130">
        <template #default="scope">
          {{ selectDictLabel(bus_currency_type, props.currencyType) }}</template
        >
      </el-table-column>
      <el-table-column prop="" label="余额" width="140">
        <template #default="scope">
          {{ scope.row.mediaBalance
          }}{{
            selectDictLabel(bus_currency_type, props.currencyType)
          }}</template
        >
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </el-dialog>
</template>

<script setup>
import { getAccountList } from "@/api//apply/index.js";
import { ref } from "vue";
import { onMounted } from "vue";
const { proxy } = getCurrentInstance();
import { selectDictLabel } from "@/utils/ruoyi.js";

const { bus_system_type, bus_currency_type } = proxy.useDict(
  "bus_system_type",
  "bus_currency_type"
);
const props = defineProps({
  value: {
    type: Boolean,
    default: false,
  },
  currencyType: {
    type: String,
    default: "",
  },
});

const total = ref(0);
const list = ref([]);
const loading = ref(false);
const selectList = ref([]);
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  status: "0",
  realName: "",
  systemType: "",
});

const emits = defineEmits(["update:value", "on-success"]);
const visible = computed({
  get() {
    return props.value;
  },
  set(val) {
    emits("update:value", val);
  },
});
const handleSelectionChange = (val) => {
  console.log(val);
  selectList.value = val;
};
const handleSearch = () => {
  queryParams.pageNum = 1;
  getList();
};
const resetSearch = () => {
  queryParams.pageNum = 1;
  getList();
};
const getList = async () => {
  try {
    loading.value = true;
    const res = await getAccountList(queryParams);
    list.value = res.rows;
    total.value = res.total;
    loading.value = false;
  } catch (error) {
    loading.value = false;
    throw new Error(error);
  }
};
const handleCancel = () => {
  visible.value = false;
};
const handleSubmit = () => {
  if (selectList.value.length === 0) {
    proxy.$message.error("请选择充值账号");
    return;
  }
  emits("on-success", selectList.value);
  visible.value = false;
};
onMounted(() => {
  getList();
});
</script>

<style scoped lang="scss">
.dialog-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  padding-bottom: 16px;
  border-bottom: 1px dashed #e4e7ed;

  .dialog-title {
    font-size: 18px;
    font-weight: 600;
    color: #303133;
  }

  .dialog-actions {
    display: flex;
    gap: 12px;
  }
}
</style>
